﻿using SD.Infrastructure.Repository.EntityFrameworkCore;
using SlamDunk.MES.Domain.Entities.PlanContxt;
using SlamDunk.MES.Domain.IRepositories.Interfaces.PlanContxt;
using SlamDunk.ValueObjects.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace SlamDunk.MES.Repository.Implements.PlanContxt
{
    /// <summary>
    /// 销售订单仓储实现
    /// </summary>
    public class SaleOrderRepository : EFAggRootRepositoryProvider<SaleOrder>, ISaleOrderRepository
    {
        #region # 分页获取销售订单列表 —— ICollection<SaleOrder> FindByPage(string keywords...
        /// <summary>
        /// 分页获取销售订单列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="customerId">客户Id</param>
        /// <param name="issuedDate">下单日期</param>
        /// <param name="deliveryDate">交付日期</param>
        /// <param name="checkStatus">审核状态</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="rowCount">总记录数</param>
        /// <param name="pageCount">总页数</param>
        /// <returns>销售订单列表</returns>
        public ICollection<SaleOrder> FindByPage(string keywords, Guid? customerId, DateTime? issuedDate, DateTime? deliveryDate, CheckStatus? checkStatus, DateTime? startTime, DateTime? endTime, int pageIndex, int pageSize, out int rowCount, out int pageCount)
        {
            issuedDate = issuedDate?.Date;
            deliveryDate = deliveryDate?.Date;
            Expression<Func<SaleOrder, bool>> condition =
                x =>
                    (string.IsNullOrEmpty(keywords) || x.Keywords.Contains(keywords)) &&
                    (customerId == null || x.CustomerId == customerId) &&
                    (issuedDate == null || x.IssuedDate == issuedDate) &&
                    (deliveryDate == null || x.DeliveryDate == deliveryDate) &&
                    (checkStatus == null || x.CheckStatus == checkStatus) &&
                    (startTime == null || x.AddedTime >= startTime) &&
                    (endTime == null || x.AddedTime <= endTime);

            IQueryable<SaleOrder> saleOrders = base.FindByPage(condition, pageIndex, pageSize, out rowCount, out pageCount);

            return saleOrders.ToList();
        }
        #endregion
    }
}
